package com.opennetexam.manager.sqlprovider;

import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 奶茶
 * Created on 2017/11/20.
 */
public class ExamComputerProvider {

    private static Logger logger = Logger.getLogger(ExamComputerProvider.class);

    public String getAllExamComputer(Map<String,Object> parameter){
        StringBuilder sb = new StringBuilder();
        String appID = (String) parameter.get("appID");
        String examSiteID = (String) parameter.get("examSiteID");
        String planId = (String) parameter.get("planId");

        sb.append("select distinct r.ExamRoomID, r.ExamRoomName, r.MachineNum,(select count(1) from ExamComputer where ExamRoomID=r.ExamRoomID)>=r.MachineNum as IsSetUp ");
        sb.append("from ExamineeArrange as a join ExamRoom as r on r.ExamRoomID=a.ExamRoomID and r.AppID=a.AppID ");
        sb.append("where a.AppID= \'").append(appID).append("\'").append(" AND a.ExamPlanInfoID= \'").append(planId).append("\' ").append(" AND r.ExamSiteID = \'").append(examSiteID).append("\'");
        sb.append(" group by r.ExamRoomID, r.ExamRoomName, r.MachineNum ");

        String sql = sb.toString();
        logger.info(sql);
        return sql;
    }

    public String findExamComputerBySomeWhere(Map<String,Object> parameter){
        StringBuilder sb = new StringBuilder();
        String examRoomID = (String) parameter.get("examRoomID");
        Integer seatNo = (Integer) parameter.get("seatNo");
        String computerIP = (String) parameter.get("computerIP");

        sb.append("SELECT * FROM ExamComputer WHERE 1=1");
        if(examRoomID != null){
            sb.append(" AND ExamRoomID = \'").append(examRoomID).append("\' ");
        }
        if(seatNo != null){
            sb.append(" AND SeatNo = ").append(seatNo);
        }
        if(computerIP != null && !("").equals(computerIP)){
            sb.append(" AND ComputerIP = \'").append(computerIP).append("\'");
        }

        String sql = sb.toString();
        logger.info(sql);
        return sql;
    }

    public String deleteExamComputerByIds(Map<String,Object> parameter){
        StringBuilder sb = new StringBuilder();
        List<Integer> ids = (List<Integer>) parameter.get("ids");
        if(ids.size() > 0){
            sb.append("DELETE FROM ExamComputer WHERE ExamComputerID in( ");
            for (int i=0;i<ids.size();i++){
                if(i != (ids.size()-1)){
                    sb.append(ids.get(i)).append(",");
                }else{
                    sb.append(ids.get(i)).append(" )");
                }
            }
        }
        String sql = sb.toString();
        logger.info(sql);
        return sql;
    }

    public static void main(String[] args) {
        Map<String,Object> map = new HashMap<>();
//        map.put("appID","1");
//        map.put("examSiteID",46);
//        map.put("planId",66);
//        getAllExamComputer(map);

        ArrayList<Integer> objects = new ArrayList<>();
        objects.add(114);
        objects.add(116);
        map.put("ids",objects);
//        deleteExamComputerByIds(map);

        map.put("examRoomID",21);
        map.put("seatNo",9);
        map.put("computerIP","192.168.1.1");
//        findExamComputerBySomeWhere(map);

    }
}
